package com.atguigu.gulimall.ware.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.atguigu.gulimall.ware.vo.SkuHasStockVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimall.ware.dao.WareSkuDao;
import com.atguigu.gulimall.ware.entity.WareSkuEntity;
import com.atguigu.gulimall.ware.service.WareSkuService;


@Service("wareSkuService")
public class WareSkuServiceImpl extends ServiceImpl<WareSkuDao, WareSkuEntity> implements WareSkuService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<WareSkuEntity> qw = new QueryWrapper<>();
        String skuId = params.get("skuId").toString();
        if (StringUtils.isNotEmpty(skuId)){
         qw.lambda().eq(WareSkuEntity::getSkuId,skuId);
        }
        String wareId = params.get("wareId").toString();
        if (StringUtils.isNotEmpty(wareId)){
            qw.lambda().eq(WareSkuEntity::getId,wareId);
        }

        IPage<WareSkuEntity> page = this.page(new Query<WareSkuEntity>().getPage(params),qw);

        return new PageUtils(page);
    }

    @Override
    public void addStock(Long skuId, Long wareId, Integer skuNum) {
        LambdaQueryWrapper<WareSkuEntity> qw = new LambdaQueryWrapper<>();
       qw.eq(WareSkuEntity::getSkuId,skuId).eq(WareSkuEntity::getWareId,wareId);
        WareSkuEntity one = this.getOne(qw);
        if (one!=null){
            this.baseMapper.updateStock(skuId,wareId,skuNum);

        }else {
            WareSkuEntity wareSkuEntity = new WareSkuEntity();
            wareSkuEntity.setSkuId(skuId);
            wareSkuEntity.setStock(skuNum);
            wareSkuEntity.setWareId(wareId);
            this.baseMapper.insert(wareSkuEntity);
        }

    }

    @Override
    public List<SkuHasStockVo> skuIdsHasStock(List<Long> skuIds) {
        List<SkuHasStockVo>  stockVos  = this.baseMapper.skuIdsHasStock(skuIds);
        return stockVos;
    }


}
